package com.ruoyi.net.utils;

import com.google.protobuf.InvalidProtocolBufferException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
import java.util.List;

public class PmDataRead {


    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static void main(String[] args) {
        String abc = "eth-10gi0/1/0/1";
        int idx = StringUtils.indexOfAny(abc, "/");
        System.out.println(abc.substring(idx-1));
        Pm.NePmHead nePmHead = null;
        /*String pmSend = "0000001c000100010000006446484d5002e1010100000011000000544a430421880031000081110000000000000000003f8001ffffff00000001000e00000000080010001880810420012a200800100018ff0120cc99b3e6fcffffffff012a0c0a0a080210041a04ffffffff32023137";
        // 将十六进制字符串转换为字节数组
        byte[] byteArray = hexStringToByteArray(pmSend);
        // 处理接收到的数据
        byte[] rData = new byte[byteArray.length-(28 + 21 + 15)];
        //从PB数据开始
        System.arraycopy(byteArray, (28 + 21 + 15), rData, 0, byteArray.length-(28 + 21 + 15));
        try {
            nePmHead = Pm.NePmHead.parseFrom(rData);
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException(e);
        }
        System.out.println(nePmHead);
        System.out.println("===========================");*/
        // 指定文件路径
        String filePath = "/Users/yy/Downloads/install/dev/ftp/upload/currentpm/pbperfm_a14002c_1.dat";
        File originalFile = new File(filePath);
        File newFile = new File(filePath + ".bak");
        try (FileInputStream fis = new FileInputStream(originalFile)) {
            // 创建一个字节数组来存储文件内容
            byte[] buffer = new byte[fis.available()];
            // 读取文件内容到字节数组
            fis.read(buffer);
            // 从PB数据开始
            // 打印字节数组的长度
//            System.out.println("文件读取成功，字节数组长度: " + buffer.length);
            nePmHead = Pm.NePmHead.parseFrom(buffer);
            System.out.println(nePmHead);

            List<Pm.BoardPm> boardPmList = nePmHead.getBrdPmList();
            for (Pm.BoardPm boardPm : boardPmList) {
                String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date(boardPm.getTime() * 1000L));
                List<Pm.PmItem> pmVectors = boardPm.getPmVectorList();
                for (Pm.PmItem pmItem : pmVectors) {
                    List<Pm.PmValueItem> pmValueItemList = pmItem.getItemList();
                    for (Pm.PmValueItem pmValueItem : pmValueItemList) {
                        //<ByteString@971d0d8 size=8 contents="B\246\000\000\000\000\000\000">
//                            long i64Value = byteArrayToI64(pmValueItem.getValue().getValue().toByteArray());
                        double i64Value = GetPMValue(pmValueItem.getValue());
                        if (0 == pmValueItem.getValue().getTag()) {

                        } else if (1 == pmValueItem.getValue().getTag()) {
                            if (pmValueItem.getValue().getLength() == 4) {
                                System.out.println(pmValueItem.getValue().getValue().toStringUtf8() + " ======= " + i64Value);
                            } else if (pmValueItem.getValue().getLength() == 8) {
                                System.out.println(pmValueItem.getValue().getValue().toStringUtf8() + " ======= " + i64Value);
                            }
                        }
                    }
                }
            }
            // 修改文件名称
            if (originalFile.renameTo(newFile)) {
                System.out.println("文件名称修改成功");
            } else {
                System.out.println("文件名称修改失败");
            }
        } catch (Exception e) {
            System.err.println("读取文件时出现错误: " + e.getMessage());
        }
    }

    private static long byteArrayToI64(byte[] m_pValue) {
        long i64Value = 0;

        ByteBuffer buffer = ByteBuffer.wrap(m_pValue);
        buffer.order(ByteOrder.BIG_ENDIAN); // 或者 ByteOrder.LITTLE_ENDIAN
        long value = buffer.getLong();

        ByteBuffer buffer1 = ByteBuffer.wrap(m_pValue);
        buffer1.order(ByteOrder.LITTLE_ENDIAN); // 或者 ByteOrder.LITTLE_ENDIAN
        long value1 = buffer1.getLong();

//        m_pValue = floatToBytes(21.05f);
       /*
        for (int i = 0; i < m_pValue.length; ++i) {
//            if(m_pValue[i] != 0) {
                // 将 i64Value 左移 8 位，然后加上当前字节的值
                i64Value = (i64Value << 8) | (m_pValue[i] & 0xFF);
//            }
        }*/
        return i64Value;
    }

    public static byte[] floatToBytes(float value) {
        ByteBuffer buffer = ByteBuffer.allocate(4); // 分配4个字节的空间
        buffer.putFloat(value); // 将浮点数放入缓冲区
        return buffer.array(); // 返回字节数组
    }

    private static final int PM_VALUE_TYPE_MIN = 0; // Replace with actual minimum value
    private static final int PM_VALUE_TYPE_MAX = 2; // Replace with actual maximum value
    private static final int PM_VALUE_TYPE_INT = 0;
    private static final int PM_VALUE_TYPE_DOUBLE = 1;
    public static double GetPMValue(Pm.TLV m_oValue) {


        // tag 0,1
        if ((int) m_oValue.getTag() <= PM_VALUE_TYPE_MIN || m_oValue.getTag() >= PM_VALUE_TYPE_MAX) {
            return 0;
        }

        long i64Value = 0;
        for (int i = 0; i < m_oValue.getLength(); ++i) {
            i64Value = (i64Value << 8) + (m_oValue.getValue().toByteArray()[i] & 0xFF);
        }

        return i64Value;
    }

    static class IDValue {
        int iValue;
        float fValue;
        long i64Value;
        double dValue;
    }



}